/*   
 * Copyright 2009-2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.fortasoft.hatfield.util.expression;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFunction;
import javax.xml.xpath.XPathFunctionResolver;

import org.fortasoft.hatfield.util.xml.XPathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FunctionResolverRegistry implements XPathFunctionResolver {
    private Map<QName, XPathFunction> fnmap = new ConcurrentHashMap<QName, XPathFunction>();
    private Logger log = LoggerFactory
            .getLogger(FunctionResolverRegistry.class);
    private List<XPathFunctionResolver> resolvers = new CopyOnWriteArrayList<XPathFunctionResolver>();

    public void attach(XPath xpath) {

        xpath.setXPathFunctionResolver(this);

    }

    public void attach(XPathUtil xpu) {
        xpu.getXPath().setXPathFunctionResolver(this);
    }

    public void addFunction(QName q, XPathFunction xpf) {
        fnmap.put(q, xpf);
    }

    public void addFunction(String ns, String name, XPathFunction xpf) {
        QName q = new QName(ns, name);
        addFunction(q, xpf);
    }

    public void addXPathFunctionResolver(XPathFunctionResolver r) {
        resolvers.add(r);
    }

    public XPathFunction resolveFunction(QName functionName, int arity) {
        HashSet<XPathFunctionResolver> set = new HashSet<XPathFunctionResolver>();
        return resolveFunction(functionName, arity, set);
    }

    private XPathFunction resolveFunction(QName functionName, int arity,
            Set<XPathFunctionResolver> set) {
        if (set.contains(this)) {
            return null;
        } else {
            set.add(this);
        }
        XPathFunction f = fnmap.get(functionName);

        if (f != null) {
            log.debug("fn " + functionName + " ==> " + f);
            return f;
        } else {
            for (XPathFunctionResolver r : resolvers) {
                if (!set.contains(r)) {
                    if (r instanceof FunctionResolverRegistry) {
                        f = ((FunctionResolverRegistry) r).resolveFunction(
                                functionName, arity, set);
                    } else {
                        f = r.resolveFunction(functionName, arity);
                    }
                    if (f != null) {
                        return f;
                    }
                }
            }
        }
        return null;
    }

}
